//
//  UIImage+MHBundle.m
//  MHCategory
//
//  Created by SY on 2018/11/27.
//

#import "NSObject+MHBundle.h"

@implementation NSObject (MHBundle)
-(UIImage *)imageBounds:(NSString *)icon boundName:(NSString *)boundName ofType:(NSString *)ext{
#ifdef isNative
        return [UIImage imageNamed:icon];
#else
    if (!ext) ext = @"png";
    NSString *img_path = [self tempPathForResource:icon ofType:ext boundName:boundName];
    return  [UIImage imageWithContentsOfFile:img_path];
#endif


}
-(NSString *)pathForResource:(NSString *)name ofType:(NSString *)ext boundName:(NSString *)boundName{
    NSBundle *bundle = [NSBundle bundleForClass:self.class];
    if (![boundName hasSuffix:@".bundle"]) {
        boundName = [NSString stringWithFormat:@"%@.bundle",boundName];
    }
    return [bundle pathForResource:name ofType:ext inDirectory:boundName];
}
- (NSString *)tempPathForResource:(NSString *)name ofType:(NSString *)ext boundName:(NSString *)boundName {

    NSString *path = [self pathForResource:name ofType:ext boundName:boundName];
    if (path) {
        return path;
    }
    CGFloat scale = [UIScreen mainScreen].scale;
    if (ABS(scale-3) <= 0.001) {
        path = [self tempPathForResource_3x:name ofType:ext boundName:boundName];
        if (!path) {
            path = [self tempPathForResource_2x:name ofType:ext boundName:boundName];
            if (!path) {
                path = [self tempPathForResource_x:name ofType:ext boundName:boundName];
            }
        }
        
    }else if (ABS(scale-2) <= 0.001){
        path = [self tempPathForResource_2x:name ofType:ext boundName:boundName];
        if (!path) {
            path = [self tempPathForResource_3x:name ofType:ext boundName:boundName];
            if (!path) {
                path = [self tempPathForResource_x:name ofType:ext boundName:boundName];
            }
        }
    }else {
        path = [self tempPathForResource_x:name ofType:ext boundName:boundName];
        if (!path) {
            path = [self tempPathForResource_2x:name ofType:ext boundName:boundName];
            if (!path) {
                path = [self tempPathForResource_3x:name ofType:ext boundName:boundName];
            }
        }
    }
    
    return path;
}

- (NSString *)tempPathForResource_x:(NSString *)name ofType:(NSString *)ext boundName:(NSString *)boundName {
    NSString *path = nil;
    NSString *teampName = nil;
    
    if ([name hasSuffix:@"@3x"]) {
        teampName = [name stringByReplacingOccurrencesOfString:@"@3x" withString:@""];
    }
    else if ([name hasSuffix:@"@2x"]) {
        teampName = [name stringByReplacingOccurrencesOfString:@"@2x" withString:@""];
    }
    else {
        teampName = name;
    }
    path = [self pathForResource:teampName ofType:ext boundName:boundName];
    
    return path;
}

- (NSString *)tempPathForResource_2x:(NSString *)name ofType:(NSString *)ext boundName:(NSString *)boundName {
    NSString *path = nil;
    NSString *teampName = nil;
    
    if ([name hasSuffix:@"@3x"]) {
        teampName = [name stringByReplacingOccurrencesOfString:@"@3x" withString:@"@2x"];
    }
    else if ([name hasSuffix:@"@2x"]) {
        teampName = name;
    }
    else {
        teampName = [NSString stringWithFormat:@"%@@2x", name];
    }
    path = [self pathForResource:teampName ofType:ext boundName:boundName];
    
    return path;
}

- (NSString *)tempPathForResource_3x:(NSString *)name ofType:(NSString *)ext boundName:(NSString *)boundName{
    NSString *path = nil;
    NSString *teampName = nil;
    
    if ([name hasSuffix:@"@3x"]) {
        teampName = name;
    }
    else if ([name hasSuffix:@"@2x"]) {
        teampName = [name stringByReplacingOccurrencesOfString:@"@2x" withString:@"@3x"];
    }
    else {
        teampName = [NSString stringWithFormat:@"%@@3x", name];
    }
    path = [self pathForResource:teampName ofType:ext boundName:boundName];
    
    return path;
}

-(NSString *)getModulePath:(NSString *)modulePath{
    if (modulePath == nil && modulePath.length == 0) {
        modulePath = @"";
    }
    if ([modulePath rangeOfString:@".bundle"].location == NSNotFound) {
        modulePath = [modulePath stringByAppendingString:@".bundle"];
    }
    NSBundle *bundle = [NSBundle bundleForClass:self.class];
    NSString *path = [bundle.resourcePath stringByAppendingPathComponent:modulePath];
    return path;
}
@end

